﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Repository.DbEntity;
using Repository.IRepository;
using StudentManage.Areas.StudentManage.Models;
using StudentManage.Areas.StudentManage.Models.Requests;

namespace StudentManage.Areas.StudentManage.Controllers
{
    /// <summary>
    /// 成绩管理
    /// </summary>
    [Area("StudentManage")]
    public class ScoreController : Controller
    {
        private readonly ILogger<ScoreController> _logger;
        private readonly IScoreRepository _scoreRepository;

        public ScoreController(ILogger<ScoreController> logger, IScoreRepository scoreRepository)
        {
            _logger = logger;
            _scoreRepository = scoreRepository;
        }

        /// <summary>
        /// 成绩管理页面
        /// </summary>
        public IActionResult Index()
        {
            return PartialView();
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> List(ReqScoreList req)
        {
            var result = new ResponseMessage();
            try
            {
                var users = await _scoreRepository.GetPageDataAsync(req.StudentName, req.Page, req.Limit);
                result.Message = "获取成功";
                result.Msg = true;
                result.Data = users.Items;
                result.Count = users.TotalCount;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }

            return Json(result);
        }

        ///// <summary>
        ///// 新增
        ///// </summary>
        //[HttpPost]
        //public async Task<IActionResult> Add(ScoreModel model)
        //{
        //    var result = new ResponseMessage();
        //    try
        //    {
        //        var score = new Score
        //        {
        //            MajorId = model.MajorId,
        //            ClassId = model.ClassId,
        //            CourseId = model.CourseId,
        //            TeacherId = model.TeacherId,
        //            Hours = model.Hours,
        //            Credit = model.Credit
        //        };
        //        await _scoreRepository.AddAsync(score);

        //        result.Message = "新增成功";
        //        result.Msg = true;

        //    }
        //    catch (Exception e)
        //    {
        //        result.Message = e.Message;
        //    }

        //    return Json(result);
        //}

        ///// <summary>
        ///// 编辑
        ///// </summary>
        //[HttpPost]
        //public async Task<IActionResult> Edit(ScoreModel model)
        //{
        //    var result = new ResponseMessage();
        //    try
        //    {

        //        var modelDb = await _scoreRepository.SingleAsync(model.Id);
        //        if (modelDb == null) return Json(result);

        //        modelDb.MajorId = model.MajorId;
        //        modelDb.ClassId = model.ClassId;
        //        modelDb.CourseId = model.CourseId;
        //        modelDb.TeacherId = model.TeacherId;
        //        modelDb.Hours = model.Hours;
        //        modelDb.Credit = model.Credit;

        //        await _scoreRepository.UpdateAsync(modelDb);

        //        result.Message = "编辑成功";
        //        result.Msg = true;
        //    }
        //    catch (Exception e)
        //    {
        //        result.Message = e.Message;
        //    }

        //    return Json(result);
        //}

        ///// <summary>
        ///// 删除
        ///// </summary>
        //[HttpPost]
        //public async Task<IActionResult> Delete(int id)
        //{
        //    var result = new ResponseMessage();
        //    try
        //    {
        //        var score = await _scoreRepository.SingleAsync(id);
        //        if (score == null) return Json(result);

        //        if (await _scoreRepository.DeleteAsync(score))
        //        {
        //            result.Message = "删除成功";
        //            result.Msg = true;
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        result.Message = e.Message;
        //    }

        //    return Json(result);
        //}
    }
}